
*******************************************************************************
* Calcualte average characteristics of homogenous relatives (siblings and cousins) 
* for each index worker. See definition of homogenous groups in main do file
*******************************************************************************

* Save a list with only coworkers in the sample to make code more efficient
use Data/data_coworkers, clear
keep PersonId
duplicates drop
save Data/tmp_data_relatives_X, replace

use Data/data_relatives, clear
merge m:1 PersonId using Data/tmp_data_relatives_X
keep if _merge == 3
drop _merge

* Add if relatives voted or not and other characteristics
rename PersonId IndexWorkerId
rename Relative PersonId
merge m:1 PersonId using Data/data_voting_2010
* This removes all relatives that were not 18 in 2010
drop if _merge == 2
drop _merge
merge m:1 PersonId using Data/data_ind_X_2010
drop if _merge == 2
drop _merge
drop P0846_lopnr_PeOrgNr WorkplaceId AstKommun Forsamling
compress

* Drop relatives that were not elegible to vote in 2010 - cannot be used to
* construct instrument
drop if Voted2010 == .

* Collapse data to mean values by index worker using only relatives which 
* belongs to the homogenous family peer group.
foreach Relative_var of varlist Voted2010-NumberOfChildren {
 rename `Relative_var' Relative_`Relative_var'
}
 
* Add characteristics of the index worker so we can see if a relative
* is in the same homogenous peer group
drop PersonId
rename IndexWorkerId PersonId 
merge m:1 PersonId using Data/data_ind_X_2017, keepusing($HPG_Relatives)
keep if _merge == 3
drop _merge

* Loop over each characteristic which defines homogenous family peers (see main)
* and keep only relatives which belong to the same homogenous family peer groups,
* ie has the same values on these characteristics
local HPG_vars $HPG_Relatives
foreach peer_var of varlist `HPG_vars' {
	local  peer_var2 = "`peer_var'"
	keep if Relative_`peer_var2' == `peer_var2'
}
drop $HPG_Relatives

gen PersonId2 = PersonId
collapse (mean)Relative_Voted2010-Relative_NumberOfChildren (count)NumberOfRelatives = PersonId2, by(PersonId)

save Data/data_relatives_X, replace















